﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using WebService;
using CTIClassLibrary;

namespace CTIPayService
{
    public partial class PaymentAmount : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string xmlNone = @"<Payment>
                    <Order>
                        <No>0</No>
                        <Amount>0</Amount>
                    </Order>
                </Payment>";

            if (Request.QueryString.Count == 0 || String.IsNullOrEmpty(Request.QueryString[0]))
            {
                //如果是坐席主动回拨给支付失败的用户，phone就会为空，这里使用坐席ip地址进行匹配，找到phone
                object phone = Common.DB.Select(Tables.t_CITIC_CallBackHistory, Tables.t_CITIC_CallBackHistory.PhoneNo)
                                        .Where(Tables.t_CITIC_CallBackHistory.RemoteIP == Request.UserHostAddress)
                                        .OrderBy(Tables.t_CITIC_CallBackHistory.Datetime.Desc).ToScalar();
                if (phone != null)
                {
                    TicketWebService service = new TicketWebService();
                    string str2 = service.PaymentAmount(phone.ToString());
                    DataEntity.XMLSchema.Payment payment = DataEntity.XMLSchema.xml_BaseClass.LoadXml<DataEntity.XMLSchema.Payment>(str2);
                    if (payment.Orders.Count > 0)
                    {
                        Common.DB.Insert(Tables.t_CITIC_CallBackHistory)
                                    .AddColumn(Tables.t_CITIC_CallBackHistory.Datetime, DateTime.Now)
                                    .AddColumn(Tables.t_CITIC_CallBackHistory.QueryString, Request.Url.AbsoluteUri)
                                    .AddColumn(Tables.t_CITIC_CallBackHistory.RemoteIP, Request.UserHostAddress)
                                    .AddColumn(Tables.t_CITIC_CallBackHistory.Remarks, "坐席回拨，重新支付")
                                    .AddColumn(Tables.t_CITIC_CallBackHistory.OrderID, payment.Orders[0].No)
                                    .AddColumn(Tables.t_CITIC_CallBackHistory.PhoneNo, phone)
                                    .Execute();
                        Response.Write(str2);
                        Response.End();
                    }
                    else
                    {
                        Response.Write(xmlNone);
                        Response.End();
                    }
                }
                else
                {

                    Common.DB.Insert(Tables.t_CITIC_CallBackHistory)
                            .AddColumn(Tables.t_CITIC_CallBackHistory.Datetime, DateTime.Now)
                            .AddColumn(Tables.t_CITIC_CallBackHistory.QueryString, Request.Url.AbsoluteUri)
                            .AddColumn(Tables.t_CITIC_CallBackHistory.RemoteIP, Request.UserHostAddress)
                            .AddColumn(Tables.t_CITIC_CallBackHistory.Remarks, "错误 没有参数")
                            .Execute();

                    Response.Write(xmlNone);
                    Response.End();
                }
            }
            else
            {
                WebService.TicketWebService tws = new WebService.TicketWebService();
                string xml = tws.PaymentAmount(Request.QueryString[0]);

                DataEntity.XMLSchema.Payment pay;
                pay = DataEntity.XMLSchema.xml_BaseClass.LoadXml<DataEntity.XMLSchema.Payment>(xml);

                if (pay.Orders.Count > 0)
                {
                    Common.DB.Insert(Tables.t_CITIC_CallBackHistory)
                            .AddColumn(Tables.t_CITIC_CallBackHistory.Datetime, DateTime.Now)
                            .AddColumn(Tables.t_CITIC_CallBackHistory.QueryString, Request.Url.AbsoluteUri)
                            .AddColumn(Tables.t_CITIC_CallBackHistory.RemoteIP, Request.UserHostAddress)
                            .AddColumn(Tables.t_CITIC_CallBackHistory.Remarks, "取订单金额")
                            .AddColumn(Tables.t_CITIC_CallBackHistory.OrderID, pay.Orders[0].No)
                            .AddColumn(Tables.t_CITIC_CallBackHistory.PhoneNo, Request.QueryString[0])
                            .Execute();
                    Response.Write(xml);
                    Response.End();
                }
                else
                {
                    Response.Write(xmlNone);
                    Response.End();
                }
            }
        }
    }
}
